package com.dru.drutils.media;

import android.content.Context;
import android.content.res.TypedArray;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TableRow;

import com.dru.drutils.R;
import com.dru.drutils.components.RemoteImageView;

/**
 * An adapter that serves RemoteImageViews, to a Gallery for instance.
 * @author Dru
 *
 */
public class RemoteImageAdapter extends BaseAdapter {
    protected int mGalleryItemBackground 	= 0;
    protected Context mContext;
    protected int width 					= 150;
    protected int height					= 100;
    private RemoteImage[] remoteImages = null;
    
    /**
     * Constructs an instance of RemoteImageAdapter with default width/height
     * @param context the context of this image adapter, so that it may spawn Views
     */
    public RemoteImageAdapter(Context context) {
        mContext = context;
        TypedArray attr = mContext.obtainStyledAttributes(R.styleable.Gallery);
        mGalleryItemBackground = attr.getResourceId( R.styleable.Gallery_galleryItemBackground, 0 );
        attr.recycle();
    }
    
    /**
     * Constructs an instance of RemoteImageAdapter with the specified width and height
     * @param context the context of this image adapter, so that it may spawn Views
     * @param width the width that can be used for Views
     * @param height the height that can be used for Views
     */
    public RemoteImageAdapter(Context context, int width, int height ) {
        mContext = context;
        TypedArray attr = mContext.obtainStyledAttributes(R.styleable.Gallery);
        mGalleryItemBackground = attr.getResourceId( R.styleable.Gallery_galleryItemBackground, 0 );
        this.width = width;
        this.height = height;
        attr.recycle();
    }

    /**
     * Gets the length of the list of URLs backing this adapter
     */
    public int getCount() {
        return this.remoteImages.length;
    }

    /**
     * Gets the item (RemoteImage) in the list at the index specified by position
     * Returns null if there is no item or if that index does not exist
     * @param position the position of the item to get
     * @return The String url at that position or null if the index does not exist
     */
    public Object getItem(int position) {
        try
        {
        	return this.remoteImages[position];
        }
        catch (Exception e)
        {
        	return null;
        }
    }

    /**
     * Sets the width for the adapter
     * @param width the width
     */
    public void setWidth( int width )
    {
    	this.width = width;
    }
    
    /**
     * Sets the height for the adapter
     * @param height the height
     */
    public void setHeight( int height )
    {
    	this.height = height;
    }
    
    /**
     * Required method, for now, only spits back the index of the item
     */
    public long getItemId(int position) {
        return position;
    }

    /**
     * Handles retrieving and/or creating the view that should be seen at position in the list
     * @param position the index in the list of the view desired
     * @param convertView a view that was cached by the user of this adapter and may or may not be up to date
     * @param parent the parent ViewGroup
     */
    public View getView(int position, View convertView, ViewGroup parent) 
    {	
    	if (convertView == null)
    	{
    		RemoteImageView imageView = new RemoteImageView(this.mContext, this.remoteImages[position]);

    		LayoutParams layoutParams = new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL); 
    		
	        imageView.setLayoutParams( new Gallery.LayoutParams( layoutParams ) );
	        imageView.setBackgroundResource( mGalleryItemBackground );
    		
	        return imageView;
    	}
    	
        return convertView;
    }
    
    /**
     * Sets the URLs, creates the RemoteImages using those URLs, and begins downloading them
     * @param urls the new list of image URLs
     */
    public void setUrls ( String[] urls )
    {
    	this.remoteImages = new RemoteImage[urls.length];
    	for (int i = 0; i < urls.length; i++)
    	{
    		this.remoteImages[i] = new RemoteImage(urls[i]);
    		
    		// might not want to do this, will see from testing
    		this.remoteImages[i].beginDownloadingImage();
    	}
    }
    
    /**
     * Set the list of remote images that should back this adapter
     * @param remoteImages the remote images to be served
     */
    public void setRemoteImages ( RemoteImage[] remoteImages )
    {
    	this.remoteImages = remoteImages;
    	
    	// if each image is not yet downloading/downloaded, it should start
    	for (int i = 0; i < this.remoteImages.length; i++)
    	{
    		if (!this.remoteImages[i].isDownloaded() && !this.remoteImages[i].isDownloading())
    		{
    			this.remoteImages[i].beginDownloadingImage();
    		}
    	}
    }
}